<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let arr = [1,2,3,4,5], size = 3;
        console.log(arr[5]);
        var chunk = function (arr, size) {
            if (arr.length === 0) return arr;
            let res = [];
            if (size > arr.length) {
                res[0] = arr;
            } else {
                let count = Math.ceil(arr.length / size);
                for (let j = 0; j < count; j++) {
                    let arr2 = [];
                    for (let i = j * size; i < j * size + size; i++) {
                        if(arr[i]==undefined) break
                        arr2.push (arr[i]) ;
                    }
                    res[j] = arr2;
                }
            }  
            console.log(res);
            return res;
        };  
        chunk(arr,size);
    </script>
</body>

</html>